home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Games / Doom / ADoom-0.8 / ADoom_src / m_misc.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  11KB  |  553 lines

  1. // Emacs style mode select   -*- C++ -*- 
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. //
  18. // $Log:$
  19. //
  20. // DESCRIPTION:
  21. //    Main loop menu stuff.
  22. //    Default Config File.
  23. //    PCX Screenshots.
  24. //
  25. //-----------------------------------------------------------------------------
  26.  
  27. static const char
  28. rcsid[] = "$Id: m_misc.c,v 1.6 1997/02/03 22:45:10 b1 Exp $";
  29.  
  30. #include <stat.h>
  31. #include <sys/types.h>
  32. #include <fcntl.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35.  
  36. #include <ctype.h>
  37.  
  38.  
  39. #include "doomdef.h"
  40.  
  41. #include "z_zone.h"
  42.  
  43. #include "m_swap.h"
  44. #include "m_argv.h"
  45.  
  46. #include "w_wad.h"
  47.  
  48. #include "i_system.h"
  49. #include "i_video.h"
  50. #include "v_video.h"
  51.  
  52. #include "hu_stuff.h"
  53.  
  54. // State.
  55. #include "doomstat.h"
  56.  
  57. // Data.
  58. #include "dstrings.h"
  59.  
  60. #include "m_misc.h"
  61.  
  62. //
  63. // M_DrawText
  64. // Returns the final X coordinate
  65. // HU_Init must have been called to init the font
  66. //
  67. extern patch_t*        hu_font[HU_FONTSIZE];
  68.  
  69. int
  70. M_DrawText
  71. ( int        x,
  72.   int        y,
  73.   boolean    direct,
  74.   char*        string )
  75. {
  76.     int     c;
  77.     int        w;
  78.  
  79.     while (*string)
  80.     {
  81.     c = toupper(*string) - HU_FONTSTART;
  82.     string++;
  83.     if (c < 0 || c> HU_FONTSIZE)
  84.     {
  85.         x += 4;
  86.         continue;
  87.     }
  88.         
  89.     w = SHORT (hu_font[c]->width);
  90.     if (x+w > SCREENWIDTH)
  91.         break;
  92.     if (direct)
  93.         V_DrawPatchDirect(x, y, 0, hu_font[c]);
  94.     else
  95.         V_DrawPatch(x, y, 0, hu_font[c]);
  96.     x+=w;
  97.     }
  98.  
  99.     return x;
  100. }
  101.  
  102.  
  103.  
  104.  
  105. //
  106. // M_WriteFile
  107. //
  108. #ifndef O_BINARY
  109. #define O_BINARY 0
  110. #endif
  111.  
  112. boolean
  113. M_WriteFile
  114. ( char const*    name,
  115.   void*        source,
  116.   int        length )
  117. {
  118.     int        handle;
  119.     int        count;
  120.     
  121.     handle = open ( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  122.  
  123.     if (handle == -1)
  124.     return false;
  125.  
  126.     count = write (handle, source, length);
  127.     close (handle);
  128.     
  129.     if (count < length)
  130.     return false;
  131.         
  132.     return true;
  133. }
  134.  
  135.  
  136. //
  137. // M_ReadFile
  138. //
  139. int
  140. M_ReadFile
  141. ( char const*    name,
  142.   byte**    buffer )
  143. {
  144.     FILE *handle;
  145.     int    count, length;
  146.     struct stat    *fileinfo;
  147.     byte        *buf;
  148.  
  149.     if ((handle = fopen (name, "r")) == NULL ||
  150.         (fileinfo = malloc (sizeof(struct stat))) == NULL ||
  151.         fstat (fileno(handle), fileinfo) == -1)
  152.         I_Error ("Couldn't read file %s", name);
  153.     length = fileinfo->st_size;
  154.     free (fileinfo);
  155.     buf = Z_Malloc (length, PU_STATIC, NULL);
  156.     count = fread (buf, 1, length, handle);
  157.     fclose (handle);
  158. /*
  159.     handle = open (name, O_RDONLY | O_BINARY, 0666);
  160.     if (handle == -1)
  161.     I_Error ("Couldn't read file %s", name);
  162.     if (fstat (handle,&fileinfo) == -1)
  163.     I_Error ("Couldn't read file %s", name);
  164.     length = fileinfo.st_size;
  165.     buf = Z_Malloc (length, PU_STATIC, NULL);
  166.     count = read (handle, buf, length);
  167.     close (handle);
  168. */
  169.     if (count < length)
  170.     I_Error ("Couldn't read file %s", name);
  171.  
  172.     *buffer = buf;
  173.     return length;
  174. }
  175.  
  176.  
  177. //
  178. // DEFAULTS
  179. //
  180. int        usemouse;
  181. int        usejoystick;
  182.  
  183. extern int    key_right;
  184. extern int    key_left;
  185. extern int    key_up;
  186. extern int    key_down;
  187.  
  188. extern int    key_strafeleft;
  189. extern int    key_straferight;
  190.  
  191. extern int    key_fire;
  192. extern int    key_use;
  193. extern int    key_strafe;
  194. extern int    key_speed;
  195.  
  196. extern int    mousebfire;
  197. extern int    mousebstrafe;
  198. extern int    mousebforward;
  199.  
  200. extern int    joybfire;
  201. extern int    joybstrafe;
  202. extern int    joybuse;
  203. extern int    joybspeed;
  204.  
  205. extern int    viewwidth;
  206. extern int    viewheight;
  207.  
  208. extern int    mouseSensitivity;
  209. extern int    showMessages;
  210.  
  211. extern int    detailLevel;
  212.  
  213. extern int    screenblocks;
  214.  
  215. extern int    showMessages;
  216.  
  217. // machine-independent sound params
  218. extern    int    numChannels;
  219.  
  220.  
  221. // UNIX hack, to be removed.
  222. #ifdef SNDSERV
  223. extern char*    sndserver_filename;
  224. extern int    mb_used;
  225. #endif
  226.  
  227. #ifdef LINUX
  228. char*        mousetype;
  229. char*        mousedev;
  230. #endif
  231.  
  232. extern char*    chat_macros[];
  233.  
  234.  
  235.  
  236. typedef struct
  237. {
  238.     char*    name;
  239.     int*    location;
  240.     int        defaultvalue;
  241.     int        scantranslate;        // PC scan code hack
  242.     int        untranslated;        // lousy hack
  243. } default_t;
  244.  
  245. default_t    defaults[] =
  246. {
  247.     {"mouse_sensitivity",&mouseSensitivity, 5},
  248.     {"sfx_volume",&snd_SfxVolume, 8},
  249.     {"music_volume",&snd_MusicVolume, 8},
  250.     {"show_messages",&showMessages, 1},
  251.     
  252.  
  253. #ifdef NORMALUNIX
  254.     {"key_right",&key_right, KEY_RIGHTARROW},
  255.     {"key_left",&key_left, KEY_LEFTARROW},
  256.     {"key_up",&key_up, KEY_UPARROW},
  257.     {"key_down",&key_down, KEY_DOWNARROW},
  258.     {"key_strafeleft",&key_strafeleft, ','},
  259.     {"key_straferight",&key_straferight, '.'},
  260.  
  261.     {"key_fire",&key_fire, KEY_RCTRL},
  262.     {"key_use",&key_use, ' '},
  263.     {"key_strafe",&key_strafe, KEY_RALT},
  264.     {"key_speed",&key_speed, KEY_RSHIFT},
  265.  
  266. // UNIX hack, to be removed. 
  267. #ifdef SNDSERV
  268.     {"sndserver", (int *) &sndserver_filename, (int) "sndserver"},
  269.     {"mb_used", &mb_used, 2},
  270. #endif
  271.     
  272. #endif
  273.  
  274. #ifdef LINUX
  275.     {"mousedev", (int*)&mousedev, (int)"/dev/ttyS0"},
  276.     {"mousetype", (int*)&mousetype, (int)"microsoft"},
  277. #endif
  278.  
  279.     {"use_mouse",&usemouse, 1},
  280.     {"mouseb_fire",&mousebfire,0},
  281.     {"mouseb_strafe",&mousebstrafe,1},
  282.     {"mouseb_forward",&mousebforward,2},
  283.  
  284. #ifdef __SASC
  285.     {"use_joystick",&usejoystick, 1},
  286. #else
  287.     {"use_joystick",&usejoystick, 0},
  288. #endif
  289.     {"joyb_fire",&joybfire,0},
  290.     {"joyb_strafe",&joybstrafe,1},
  291.     {"joyb_use",&joybuse,3},
  292.     {"joyb_speed",&joybspeed,2},
  293.  
  294.     {"screenblocks",&screenblocks, 9},
  295.     {"detaillevel",&detailLevel, 0},
  296.  
  297. #ifdef __SASC
  298.     {"snd_channels",&numChannels, 2},   /* 2 for effects + 2 for music */
  299. #else
  300.     {"snd_channels",&numChannels, 3},
  301. #endif
  302.  
  303.  
  304.     {"usegamma",&usegamma, 0},
  305.  
  306.     {"chatmacro0", (int *) &chat_macros[0], (int) HUSTR_CHATMACRO0 },
  307.     {"chatmacro1", (int *) &chat_macros[1], (int) HUSTR_CHATMACRO1 },
  308.     {"chatmacro2", (int *) &chat_macros[2], (int) HUSTR_CHATMACRO2 },
  309.     {"chatmacro3", (int *) &chat_macros[3], (int) HUSTR_CHATMACRO3 },
  310.     {"chatmacro4", (int *) &chat_macros[4], (int) HUSTR_CHATMACRO4 },
  311.     {"chatmacro5", (int *) &chat_macros[5], (int) HUSTR_CHATMACRO5 },
  312.     {"chatmacro6", (int *) &chat_macros[6], (int) HUSTR_CHATMACRO6 },
  313.     {"chatmacro7", (int *) &chat_macros[7], (int) HUSTR_CHATMACRO7 },
  314.     {"chatmacro8", (int *) &chat_macros[8], (int) HUSTR_CHATMACRO8 },
  315.     {"chatmacro9", (int *) &chat_macros[9], (int) HUSTR_CHATMACRO9 }
  316.  
  317. };
  318.  
  319. int    numdefaults;
  320. char*    defaultfile;
  321.  
  322.  
  323. //
  324. // M_SaveDefaults
  325. //
  326. void M_SaveDefaults (void)
  327. {
  328.     int        i;
  329.     int        v;
  330.     FILE*    f;
  331.     
  332.     f = fopen (defaultfile, "w");
  333.     if (!f)
  334.     return; // can't write the file, but don't complain
  335.         
  336.     for (i=0 ; i<numdefaults ; i++)
  337.     {
  338.     if (defaults[i].defaultvalue > -0xfff
  339.         && defaults[i].defaultvalue < 0xfff)
  340.     {
  341.         v = *defaults[i].location;
  342.         fprintf (f,"%s\t\t%i\n",defaults[i].name,v);
  343.     } else {
  344.         fprintf (f,"%s\t\t\"%s\"\n",defaults[i].name,
  345.              * (char **) (defaults[i].location));
  346.     }
  347.     }
  348.     
  349.     fclose (f);
  350. }
  351.  
  352.  
  353. //
  354. // M_LoadDefaults
  355. //
  356. extern byte    scantokey[128];
  357.  
  358. void M_LoadDefaults (void)
  359. {
  360.     int        i;
  361.     int        len;
  362.     FILE*    f;
  363.     char    def[80];
  364.     char    strparm[100];
  365.     char*    newstring;
  366.     int        parm;
  367.     boolean    isstring;
  368.     
  369.     // set everything to base values
  370.     numdefaults = sizeof(defaults)/sizeof(defaults[0]);
  371.     for (i=0 ; i<numdefaults ; i++)
  372.     *defaults[i].location = defaults[i].defaultvalue;
  373.     
  374.     // check for a custom default file
  375.     i = M_CheckParm ("-config");
  376.     if (i && i<myargc-1)
  377.     {
  378.     defaultfile = myargv[i+1];
  379.     printf ("    default file: %s\n",defaultfile);
  380.     }
  381.     else
  382.     defaultfile = basedefault;
  383.     
  384.     // read the file in, overriding any set defaults
  385.     f = fopen (defaultfile, "r");
  386.     if (f)
  387.     {
  388.     while (!feof(f))
  389.     {
  390.         isstring = false;
  391.         if (fscanf (f, "%79s %[^\n]\n", def, strparm) == 2)
  392.         {
  393.         if (strparm[0] == '"')
  394.         {
  395.             // get a string default
  396.             isstring = true;
  397.             len = strlen(strparm);
  398.             newstring = (char *) malloc(len);
  399.             strparm[len-1] = 0;
  400.             strcpy(newstring, strparm+1);
  401.         }
  402.         else if (strparm[0] == '0' && strparm[1] == 'x')
  403.             sscanf(strparm+2, "%x", &parm);
  404.         else
  405.             sscanf(strparm, "%i", &parm);
  406.         for (i=0 ; i<numdefaults ; i++)
  407.             if (!strcmp(def, defaults[i].name))
  408.             {
  409.             if (!isstring)
  410.                 *defaults[i].location = parm;
  411.             else
  412.                 *defaults[i].location =
  413.                 (int) newstring;
  414.             break;
  415.             }
  416.         }
  417.     }
  418.         
  419.     fclose (f);
  420.     }
  421. }
  422.  
  423.  
  424. //
  425. // SCREEN SHOTS
  426. //
  427.  
  428.  
  429. typedef struct
  430. {
  431.     char        manufacturer;
  432.     char        version;
  433.     char        encoding;
  434.     char        bits_per_pixel;
  435.  
  436.     unsigned short    xmin;
  437.     unsigned short    ymin;
  438.     unsigned short    xmax;
  439.     unsigned short    ymax;
  440.     
  441.     unsigned short    hres;
  442.     unsigned short    vres;
  443.  
  444.     unsigned char    palette[48];
  445.     
  446.     char        reserved;
  447.     char        color_planes;
  448.     unsigned short    bytes_per_line;
  449.     unsigned short    palette_type;
  450.     
  451.     char        filler[58];
  452.     unsigned char    data;        // unbounded
  453. } pcx_t;
  454.  
  455.  
  456. //
  457. // WritePCXfile
  458. //
  459. void
  460. WritePCXfile
  461. ( char*        filename,
  462.   byte*        data,
  463.   int        width,
  464.   int        height,
  465.   byte*        palette )
  466. {
  467.     int        i;
  468.     int        length;
  469.     pcx_t*    pcx;
  470.     byte*    pack;
  471.     
  472.     pcx = Z_Malloc (width*height*2+1000, PU_STATIC, NULL);
  473.  
  474.     pcx->manufacturer = 0x0a;        // PCX id
  475.     pcx->version = 5;            // 256 color
  476.     pcx->encoding = 1;            // uncompressed
  477.     pcx->bits_per_pixel = 8;        // 256 color
  478.     pcx->xmin = 0;
  479.     pcx->ymin = 0;
  480.     pcx->xmax = SHORT(width-1);
  481.     pcx->ymax = SHORT(height-1);
  482.     pcx->hres = SHORT(width);
  483.     pcx->vres = SHORT(height);
  484.     memset (pcx->palette,0,sizeof(pcx->palette));
  485.     pcx->color_planes = 1;        // chunky image
  486.     pcx->bytes_per_line = SHORT(width);
  487.     pcx->palette_type = SHORT(2);    // not a grey scale
  488.     memset (pcx->filler,0,sizeof(pcx->filler));
  489.  
  490.  
  491.     // pack the image
  492.     pack = &pcx->data;
  493.     
  494.     for (i=0 ; i<width*height ; i++)
  495.     {
  496.     if ( (*data & 0xc0) != 0xc0)
  497.         *pack++ = *data++;
  498.     else
  499.     {
  500.         *pack++ = 0xc1;
  501.         *pack++ = *data++;
  502.     }
  503.     }
  504.     
  505.     // write the palette
  506.     *pack++ = 0x0c;    // palette ID byte
  507.     for (i=0 ; i<768 ; i++)
  508.     *pack++ = *palette++;
  509.     
  510.     // write output file
  511.     length = pack - (byte *)pcx;
  512.     M_WriteFile (filename, pcx, length);
  513.  
  514.     Z_Free (pcx);
  515. }
  516.  
  517.  
  518. //
  519. // M_ScreenShot
  520. //
  521. void M_ScreenShot (void)
  522. {
  523.     int        i;
  524.     byte*    linear;
  525.     char    lbmname[12];
  526.     
  527.     // munge planar buffer to linear
  528.     linear = screens[2];
  529.     I_ReadScreen (linear);
  530.     
  531.     // find a file name to save it to
  532.     strcpy(lbmname,"DOOM00.pcx");
  533.         
  534.     for (i=0 ; i<=99 ; i++)
  535.     {
  536.     lbmname[4] = i/10 + '0';
  537.     lbmname[5] = i%10 + '0';
  538.     if (access(lbmname,0) == -1)
  539.         break;    // file doesn't exist
  540.     }
  541.     if (i==100)
  542.     I_Error ("M_ScreenShot: Couldn't create a PCX");
  543.     
  544.     // save the pcx file
  545.     WritePCXfile (lbmname, linear,
  546.           SCREENWIDTH, SCREENHEIGHT,
  547.           W_CacheLumpName ("PLAYPAL",PU_CACHE));
  548.     
  549.     players[consoleplayer].message = "screen shot";
  550. }
  551.  
  552.  
  553.